#!/usr/bin/python
#=00
import sys#--
import pygst#--
pygst.require("0.10")#--
import gst#--
import pygtk#--
pygtk.require("2.0")#--
import gtk#--

class Video:#--
    def __init__(self):
        self.pipeline = gst.Pipeline()
        self.bus = self.pipeline.get_bus()
        self.bus.add_watch(self.busMessage)
        self.filesrc = gst.element_factory_make("filesrc")
        decoder = gst.element_factory_make("decodebin", "decode")
        decoder.connect("new-decoded-pad", self.padCreated)
        decoder.connect("unknown-type", self.unknownFile)
        self.pipeline.add(self.filesrc, decoder)
        self.filesrc.link(decoder)
        self.audioQ = gst.element_factory_make("queue")
        convert = gst.element_factory_make("audioconvert")
        audiosink = gst.element_factory_make("alsasink")
	audiosink.set_property("device", "plughw:2")
        self.videoQ = gst.element_factory_make("queue")
#=01 video!
        videosink = gst.element_factory_make("xvimagesink") #01
        self.pipeline.add(self.audioQ, convert, audiosink, 
                          self.videoQ, videosink)
        gst.element_link_many(self.audioQ, convert, audiosink)
        gst.element_link_many(self.videoQ, videosink)

    def padCreated(self, bin, pad, islast):
        audioPad = self.audioQ.get_pad("sink")
        videoPad = self.videoQ.get_pad("sink")
        if pad.get_caps().intersect(audioPad.get_caps()):
            pad.link(audioPad)
        if pad.get_caps().intersect(videoPad.get_caps()):
            pad.link(videoPad)
#--
    def unknownFile(self, bin, pad, caps):#--
        print "unknown file type!", sys.argv[1]#--
        # Happens before the pipeline starts running#--
        sys.exit(0)#--
#--
    def busMessage(self, bus, message):#--
        if message.type == gst.MESSAGE_EOS:#--
            gtk.main_quit()#--
        return True#--
#--
    def run(self, filename):#--
        self.filesrc.set_property("location", filename)#--
        self.pipeline.set_state(gst.STATE_PLAYING)#--
        gtk.main()#--
#--
if __name__ == "__main__":#--
    video=Video()#--
    video.run(sys.argv[1])#--
